package com.insurance.web;

import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import com.insurance.bean.Agent;
import com.insurance.bean.Blog;
import com.insurance.bean.Caze;
import com.insurance.bean.Company;
import com.insurance.bean.Product;
import com.insurance.service.AgentService;
import com.insurance.service.BlogService;
import com.insurance.service.CazeService;
import com.insurance.service.CompanyService;
import com.insurance.service.ProductService;


public class AgentAction {
	@Resource AgentService agentService;
	@Resource BlogService blogService;
	@Resource CazeService cazeService;
	@Resource CompanyService companyService;
	@Resource ProductService productService;
	private String message;
	private List<Agent> agents;
	private List<Agent> agents1;
	private List<Blog> blogs;
	private List<Caze> cazes;
	private List<Company> companys;
	private List<Product> products;
	private Agent agent;
	private Integer id;
	private String userName;

	
	public List<Agent> getAgents1() {
		return agents1;
	}
	public void setAgents1(List<Agent> agents1) {
		this.agents1 = agents1;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public List<Caze> getCazes() {
		return cazes;
	}
	public void setCazes(List<Caze> cazes) {
		this.cazes = cazes;
	}
	public Integer getId(){
		return id;
	}
	public void setId(Integer id){
		this.id = id;
	}
	public Agent getAgent(){
		return agent;
	}
	public void setAgent(Agent agent){
		this.agent = agent;
	}
	
	/**
	 * 
	 * 显示所有经纪人
	 */
	public String list(){
		this.agents = agentService.getAgentsChecked();
		return "list";
	}

	/**
	 * 
	 * 经纪人登录界面
	 */
	public String loginUI(){
		Agent agent = (Agent)ServletActionContext.getRequest().getSession().getAttribute("agent");
		if(agent==null)
		return "login";
		else 
			return "home";
	}
	
	/**
	 * 
	 * 经纪人登录
	 */
	public String verify(){
		Agent agent1 = agentService.verifyAgent(agent);
		if(agent1 != null){
			ServletActionContext.getRequest().getSession().setAttribute("agent", agent1);
			this.blogs = this.blogService.getBlogsByWriter(agent.getName());
			this.cazes = this.cazeService.getCazesByWriter(agent.getName());
			this.products = this.productService.getProductsByAgent(agent.getId());
			return "success";
			}
			else 
				
				return "fail";
	}
	/**
	 *verify userName available 
	 */
	public void verifyUserName(){
		this.agentService.verifyUserName(this.userName);
	}
	/**
	 * 
	 * 显示单个经纪人
	 */
	public String showAgent(){
		this.agent = this.agentService.getAgent(this.id);
		return "show";
	}
	
	/**
	 *search agent by area and company 
	 */
	public String searchAgent(){
		this.agents = this.agentService.searchAgent(agent);
		return "search";
	}
	
	/**
	 *
	 * 经纪人修改界面
	 */
	public String modifyUI(){
		this.agent = this.agentService.getAgent(this.id);
		return "modify";
	}
	
	/**
	 * 
	 * 修改经纪人
	 */
	public String modify(){
		this.agentService.update(agent);
		this.message="修改成功！";
		return "message";
	}
	
	/**
	 *
	 * 增加经纪人界面
	 */
	public String addUI(){
		this.companys = this.companyService.getCompanys();
		return "add";
	}
	
	/**
	 *
	 * 增加经纪人
	 */
	public String add(){
		this.agentService.save(this.agent);
		this.message="注册成功！";
		return "message";
	}
	
	/**
	 * 
	 * 删除经纪人
	 */
	public String del(){
		this.agentService.delete(this.id);
		this.message="删除成功！";
		return "message";
	}
	
	/**
	 * 
	 * 经纪人主页面
	 */
	public String index(){
		this.companys = this.companyService.getCompanys();
		this.agents = this.agentService.getAgentsChecked();
		this.agents1 = this.agentService.getAgentsCheckedTop5();
		return "index";
	}
	
	/**
	 *经纪人退出登录 
	 */
	public String logout(){
		ServletActionContext.getRequest().getSession().removeAttribute("agent");
		return "agentlogin";
	}
	
	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	public void setBlogs(List<Blog> blogs) {
		this.blogs = blogs;
	}
	public List<Blog> getBlogs() {
		return blogs;
	}
	
}
